package com.maxiaoke.boot.controller;

import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class ParamController {

    @GetMapping("/car/{id}/owner/{name}")
    public Map<String, Object> getCar(
            @PathVariable("id") Integer id,
            @PathVariable("name") String name,
            @PathVariable Map<String, String> kv
    ) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        map.put("name", name);
        map.put("kv", kv);

        return map;
    }

    @GetMapping("/header")
    public Map<String, Object> getHeader(
            @RequestHeader("User-Agent") String userAgent,
            @RequestHeader Map<String, String> header
    ) {
        Map<String, Object> map = new HashMap<>();
        map.put("userAgent", userAgent);
        map.put("header", header);

        return map;
    }

    @GetMapping("/testparam")
    public Map<String, Object> getParam(
            @RequestParam Integer age,
            @RequestParam List<String> ins,
            @RequestParam Map<String, String> params
    ) {
        Map<String, Object> map = new HashMap<>();
        map.put("age", age);
        map.put("ins", ins);
        map.put("params", params);

        return map;
    }


    @GetMapping("/testcookie")
    public Map<String, Object> testCookie(
            @CookieValue("_ga") String _ga,
            @CookieValue("_ga") Cookie cookie
    ) {
        Map<String, Object> map = new HashMap<>();
        map.put("_ga", _ga);
        System.out.println(cookie.getName() + "===>" + cookie.getValue());

        return map;
    }

    @PostMapping("/testpost")
    public Map<String, Object> testPost(
            @RequestBody String content
    ) {
        Map<String, Object> map = new HashMap<>();
        map.put("content", content);

        return map;
    }


}
